﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using WCFPointOfSaleService.Entities.Parameters;
using WCFPointOfSaleService.Entities;
using WCFPointOfSaleService.Class;

namespace WCFPointOfSaleService
{
    public class StockService : IStockService
    {
        public ObjectResult<object> SetSyncTrue(ParamStockToSyncTrue param)
        {
            ObjectResult<object> result = new ObjectResult<object>();
            bool exists = false;
            try
            {
                if (Validation.checkParameterRequired(param))
                {
                    EntityFrameworkManager.StockLogByUuid stockLogbyUuidEntity;


                    foreach (StockToSyncTrue item in param.StockToSyncTrueList)
                    {
                        exists = false;
                        using (var entity = new EntityFrameworkManager.FalconSystemDBEntities())
                        {
                            stockLogbyUuidEntity = new EntityFrameworkManager.StockLogByUuid();

                            stockLogbyUuidEntity.CompanyID = item.companyId;
                            stockLogbyUuidEntity.Uuid = item.uuidTransaction;
                            stockLogbyUuidEntity.StockID = item.stockId;
                            stockLogbyUuidEntity.Tick = item.Tick;
                            stockLogbyUuidEntity.TransactionDate = item.TransactionDate;
                            stockLogbyUuidEntity.IsSync = true;

                            try
                            {
                                entity.StockLogByUuid.Add(stockLogbyUuidEntity);
                                entity.SaveChanges();
                            }
                            catch (Exception ex)
                            {
                                if (ex.InnerException != null)
                                {
                                    if (ex.InnerException.ToString().ToUpper().Contains("VIOLATION OF PRIMARY KEY"))
                                    {
                                        exists = true;
                                    }
                                    else
                                        throw ex;
                                }
                                else
                                    throw ex;
                            }
                        }

                        if (exists) {
                            using (var entity = new EntityFrameworkManager.FalconSystemDBEntities())
                            {
                                entity.Custom_StockLogByUuid_UpdateSync(item.companyId, item.stockId, item.TransactionDate, item.uuidTransaction, item.Tick);
                                entity.SaveChanges();
                            }
                        }

                    }
                }
            }
            catch (ParameterException objex)
            {
                //devuelve el error de parametros
                result.code = objex.Code;
                result.isSuccessful = false;
                result.description = objex.Message;
            }
            catch (Exception ex)
            {
                result.code = "99";
                result.description = "No se logró indicar a los productos que ya fueron sincronizados";
                result.descriptionDetail = "Error Message: " + ex.Message != null ? ex.Message : string.Empty + " Error Exception: " + ex.InnerException != null ? ex.InnerException.ToString() : string.Empty;
                result.isSuccessful = false;
            }
            return result;
        }

    }
}
